//
//  GuidePageView.m
//
//  Created by Zy on 2017/6/17.
//  Copyright © 2017年 Zy. All rights reserved.
//

#import "GuidePageView.h"

@interface GuidePageView () <UIScrollViewDelegate>
{
    UIPageControl *_page;
}
@property (nonatomic, strong) UIButton *startButton;
@property (nonatomic, copy) GuidePageComplete complete;

@end

@implementation GuidePageView

#pragma mark - instancetype
+ (instancetype)guidePageViewComplete:(GuidePageComplete)complete {
    GuidePageView *view = [[self alloc] init];
    view.complete = complete;
    return view;
}

#pragma mark - init
- (instancetype)init {
    if (self = [super init]) {
        // 弹簧效果.
        self.bounces = NO;
        // 分页显示.
        self.pagingEnabled = YES;
        // 水平滚动条.
        self.showsHorizontalScrollIndicator = NO;
        // 代理.
        self.delegate = self;
        // 显示区域.
        self.frame = (CGRect){CGPointZero, [UIScreen mainScreen].bounds.size};
        [[UIApplication sharedApplication].windows.lastObject addSubview:self];
    }
    return self;
}

#pragma mark - PageControl
- (void)createPageControl {
    _page = [[UIPageControl alloc] init];
    _page.bounds = (CGRect){CGPointZero, 150, 0};
    _page.center = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.95);
    [[UIApplication sharedApplication].windows.lastObject addSubview:_page];
}

#pragma mark - Setter
- (void)setImageArray:(NSArray *)imageArray {
    _imageArray = imageArray;
    if (imageArray.count) {
        self.contentSize = CGSizeMake((_imageArray.count + !_showStartButton) * self.frame.size.width, 0);
        [self addGuideImage];
    }
}

- (void)setPlistFile:(NSString *)plistFile {
    _plistFile = plistFile;
    NSString *filePath = [[NSBundle mainBundle] pathForResource:plistFile ofType:nil];
    self.imageArray = [NSArray arrayWithContentsOfFile:filePath];
}

- (void)setShowStartButton:(BOOL)showStartButton {
    _showStartButton = showStartButton;
    if (showStartButton) {
        _page.numberOfPages = _imageArray.count;
        for (NSInteger i = 0; i < self.subviews.count; i++) {
            if (i == _imageArray.count) {
                UIImageView *imageView = self.subviews[i];
                imageView.userInteractionEnabled = YES;
                [imageView addSubview:self.startButton];
            }
        }
    } else {
        if (_startButton) {
            [_startButton removeFromSuperview];
            _startButton = nil;
        }
        if (_page) {
            _page.numberOfPages = _imageArray.count + !_showStartButton;
        }
    }
}

- (void)setShowPage:(BOOL)showPage {
    _showPage = showPage;
    if (showPage) {
        [self createPageControl];
        _page.numberOfPages = _imageArray.count + !_showStartButton;
    } else {
        if (_page) {
            [_page removeFromSuperview];
            _page = nil;
        }
    }
}

- (void)setStartButtonImage:(NSString *)image {
    if (image.length) {
        CGSize size = self.frame.size;
        UIImage *buttonImage = [UIImage imageNamed:image];
        [self.startButton setImage:buttonImage forState:UIControlStateNormal];
        CGFloat buttonW = buttonImage.size.width * 0.6;
        CGFloat buttonH = buttonImage.size.height * 0.6;
        CGFloat buttonX = (size.width - buttonW) * 0.5;
        self.startButton.frame = CGRectMake(buttonX, size.height * 0.83, buttonW, buttonH);
    }
}

#pragma mark - Getter
- (UIButton *)startButton {
    if (!_startButton) {
           _startButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [_startButton addTarget:self action:@selector(startEvent) forControlEvents:UIControlEventTouchUpInside];
    }
    return _startButton;
}

#pragma mark - AddGuideImage
- (void)addGuideImage {
    CGSize size = self.frame.size;
    for (NSInteger i = 0; i < _imageArray.count; i++) {
        CGFloat imageViewX = i * size.width;
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.tag = i;
        imageView.image = [UIImage imageNamed:_imageArray[i]];
        imageView.frame = (CGRect){imageViewX, 0, size};
        [self addSubview:imageView];
    }
}

#pragma mark - scroll view delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    _page.currentPage = scrollView.contentOffset.x / scrollView.frame.size.width;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    BOOL boolean = scrollView.contentOffset.x / scrollView.frame.size.width == _imageArray.count;
    if (!_showStartButton && boolean) {
        [self startEvent];
    }
}

#pragma mark - StartEvent
- (void)startEvent {
    if (_complete) {
        __weak typeof(self) weakSelf = self;
        _complete(weakSelf);
    }
    if ([self.guidePageDelegate respondsToSelector:@selector(guidePageViewComplete:)])
        [self.guidePageDelegate guidePageViewComplete:self];
    if (_page) {
        [_page removeFromSuperview];
        _page = nil;
    }
    if (_startButton) {
        [_startButton removeFromSuperview];
        _startButton = nil;
    }
    [self removeFromSuperview];
}

- (void)dealloc {
//    NSLog(@"----- GuidePageView Dealloc -----");
}

@end
